package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Statistics;
import cn.itcast.service.MemberStatisticsService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class MemberStatisticsController {

    @Reference
    private MemberStatisticsService memberStatisticsService;

    /**
     * 会员数量统计  查询日期和次数
     *
     * @return
     */
    @GetMapping("/member/count")
    public Result MemberStatistics() {
        try {
            Map<String, Object> map = new HashMap();
            List<Integer> count = new ArrayList<>();
            List<String> dete = new ArrayList<>();

            List<Statistics> list = memberStatisticsService.selectDateorCount();
            for (Statistics statistics : list) {
                String months = statistics.getMonths();
                dete.add(months);
                Integer counts = statistics.getCount();
                count.add(counts);
                map.put("months", dete);
                map.put("memberCount", count);
            }


            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }


    /*
     * 查询套餐预约统计
     *
     * @return*/

    @GetMapping("/setmeals")
    public Result PackageReservation() {
        try {
            Map<String, Object> map = new HashMap();
            List<Map<String, Object>> setmealCount = memberStatisticsService.selectSetMealorCount();

            List<String> setmealNames = new ArrayList<>();
            for (Map<String, Object> objectMap : setmealCount) {
                String name = (String) objectMap.get("name");
                setmealNames.add(name);
            }

            map.put("setmealNames", setmealNames);
            map.put("setmealCount", setmealCount);
            System.out.println(map);
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }

    }

    /**
     * 运用数据统计
     *
     * @return
     */
    @RequestMapping("/business")
    public Result business () {

        try {
            Map<String, Object> data = this.memberStatisticsService.findBusiness();

            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }


    /**
     * 导出运营数据
     *
     * @param response
     * @return
     */
    @RequestMapping("/export/business")
    public Result exportbusiness(HttpServletResponse response) {
        try {
            Map<String, Object> data = this.memberStatisticsService.findBusiness();
            String reportDate = (String) data.get("reportDate");
            Integer todayNewMember = (Integer) data.get("todayNewMember");
            Integer totalMember = (Integer) data.get("totalMember");
            Integer thisWeekNewMember = (Integer) data.get("thisWeekNewMember");
            Integer thisMonthNewMember = (Integer) data.get("thisMonthNewMember");
            Integer todayOrderNumber = (Integer) data.get("todayOrderNumber");
            Integer todayVisitsNumber = (Integer) data.get("todayVisitsNumber");
            Integer thisWeekOrderNumber = (Integer) data.get("thisWeekOrderNumber");
            Integer thisWeekVisitsNumber = (Integer) data.get("thisWeekVisitsNumber");
            Integer thisMonthOrderNumber = (Integer) data.get("thisMonthOrderNumber");
            Integer thisMonthVisitsNumber = (Integer) data.get("thisMonthVisitsNumber");
            List<Map> list = (List<Map>) data.get("hotSetmeal");

//            String file = "D:\\ideaProjects\\health\\itcast-health-parent\\itcast-health-backend\\src\\main\\resources\\static\\template\\report_template.xlsx";
            String file = "C:\\Users\\Administrator\\Desktop\\czbk\\itcast-health-parent\\itcast-health-backend\\src\\main\\resources\\static\\template\\report_template.xlsx";
            //创建表格对象
            XSSFWorkbook xk = new XSSFWorkbook(new FileInputStream(new File(file)));
            //读取第一个工作表
            XSSFSheet sheetAt = xk.getSheetAt(0);
            XSSFRow row = sheetAt.getRow(2);
            XSSFCell cell = row.getCell(5);
            cell.setCellValue(reportDate);//报表日期

            row = sheetAt.getRow(4);
            row.getCell(5).setCellValue(todayNewMember);//本日新增会员数
            row.getCell(7).setCellValue(totalMember);//总会员数

            row = sheetAt.getRow(5);
            row.getCell(5).setCellValue(thisWeekNewMember);//本周新增会员数
            row.getCell(7).setCellValue(thisMonthNewMember);//本月新增会员数

            row = sheetAt.getRow(7);
            row.getCell(5).setCellValue(todayOrderNumber);//本日预约数
            row.getCell(7).setCellValue(todayVisitsNumber);//本日到诊数

            row = sheetAt.getRow(8);
            row.getCell(5).setCellValue(thisWeekOrderNumber);//本周预约数
            row.getCell(7).setCellValue(thisWeekVisitsNumber);//本周到诊数

            row = sheetAt.getRow(9);
            row.getCell(5).setCellValue(thisMonthOrderNumber);//本月预约数
            row.getCell(7).setCellValue(thisMonthVisitsNumber);//本月到诊数

            int Num = 12;
            for (Map map : list) {
                String name = (String) map.get("name");
                Long setmeal_count = (Long) map.get("setmeal_count");
                BigDecimal proportion = (BigDecimal) map.get("proportion");
                row = sheetAt.getRow(Num++);
                row.getCell(4).setCellValue(name);//套餐数
                row.getCell(5).setCellValue(setmeal_count);//预约数
                row.getCell(6).setCellValue(proportion.doubleValue());//占比例
            }
            //通过输出流进行表下载
            OutputStream out = response.getOutputStream();
            //设置响应信息
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
            xk.write(out);

            out.flush();
            out.close();
            xk.close();

            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }

    }


